const { ccclass, property } = cc._decorator;

@ccclass
export default class EnemyControl extends cc.Component {
  @property(cc.Label)
  label: cc.Label = null;

  @property
  text: string = "hello";

  //预设体子弹
  @property(cc.Prefab)
  bulletPre: cc.Prefab = null;

  // LIFE-CYCLE CALLBACKS:

  // onLoad () {}

  isDie: boolean = false;
  start() {
    //自动攻击 计时器
    this.schedule(() => {
      // 创建子弹
      let bullet = cc.instantiate(this.bulletPre);
      //设置父物体
      bullet.setParent(cc.director.getScene());
      // 设置位置
      bullet.x = this.node.x;
      bullet.y = this.node.y - 60;
    }, 0.5);
    // 开启碰撞检测
    //   cc.director.getCollisionManager().enabled = true;
  }

  update(dt) {
    if (!this.isDie) {
      this.node.y -= 100 * dt;
    }
    if (this.node.y < -850) {
      this.node.destroy();
    }
  }

  //死亡
  die() {
    //加载爆炸图片
    cc.loader.loadRes("enemy0_die", cc.SpriteFrame, (err, res) => {
      this.node.getComponent(cc.Sprite).spriteFrame = res;
      this.isDie = true;
    });
    //300毫秒后销毁
    setTimeout(() => {
      if (this.node) {
        this.node.destroy();
      }
    }, 300);
  }
  onCollisionEnter(other) {
    /**@tag
     * 1玩家子弹
     * 2敌机
     * 3敌方子弹
     * 4玩家
     */
    // 子弹打中后销毁
    if (other.tag == 1 || other.tag == 4) {
      this.die();
    }
  }
}
